//app.js
var saveData = require('utils/saveData.js');

App({
  onLaunch: function () {
    var that=this;
    var userId = saveData.getObject("userId");
    if (userId != "") {
      console.log("userid获取成功")
      this.globalData.userId=userId
      wx.request({
        url: 'https://shian.xiaoyugongxiang.com:5443/finduser',
        data: {
          wechatid: userId
        },
        method: "POST",
        header:
        {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res.data)
          if (res.data.resultCode == "0")
            that.globalData.balance=(res.data.resultMsg / 100).toFixed(1)
          else
            console.log("not found user")
        }
      })
    }
    else {
      console.log("userId为空")
      wx.login({
        success(res) {
          wx.request({
            url: 'https://shian.xiaoyugongxiang.com:5443/getopenid', 
            data: {
              jscode: res.code
            },
            method: "POST",
            header:
            {
              'content-type': 'application/json'
            },
            success: function (res)
            {
              console.log(res.data)
              if (res.data.resultCode == "0")
              {
                 saveData.saveObject(res.data.resultMsg, "userId");
                that.globalData.userId = res.data.resultMsg;
              }
              else
                console.log("error")
            }
          })
          wx.request({
            url: 'https://shian.xiaoyugongxiang.com:5443/finduser',
            data: {
              wechatid: userId
            },
            method: "POST",
            header:
            {
              'content-type': 'application/json'
            },
            success: function (res) {
              console.log(res.data)
              if (res.data.resultCode == "0")
                that.globalData.balance = (res.data.resultMsg / 100).toFixed(1)
              else
                console.log("not found user")
            }
          })
        
        }
      })
    }
  }, 
  globalData:{
    userId:"",
    balance: 0,
    isLogin: false,
    //用户个人信息
    userInfo: {
      avatarUrl: "",//用户头像
      nickName: "",//用户昵称
    }

  }
})